package com.acme.bankapp.domain.bank;

import com.acme.bankapp.service.bank.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {
	// using protected to allow subclasses to modify it
	protected int id;
	protected double balance;

	// using final to prevent confusion caused by unexpectedly changed value of
	// variable
	public AbstractAccount() {
	}

	public AbstractAccount(final double amount) throws IllegalArgumentException {
		this.balance = amount;
		if (amount < 0)
			throw new IllegalArgumentException(
					"Account is not added! Balance is negative");
	}
	
	public long decimalValue() {
		return Math.round(balance);
	}

	public void deposit(final double amount) {
		balance += amount;
		System.out.println("Successfully, " + amount
				+ " was deposited to account");
	}

	public void withdraw(final double amount) throws NotEnoughFundsException {
		if (this.balance >= amount) {
			this.balance -= amount;
			System.out.println("Successfully, " + amount
					+ " was withdrawn from account");
		} else {
			throw new NotEnoughFundsException(
					"Not enough money in the account: requested " + amount
							+ ", have " + this.balance + " only");
		}
	}

	public double maximumAmountToWithdraw() {
		return this.balance;
    }
	
	public double getBalance() {
		return balance;
	}
	
	public String toString() {
		return "" + balance;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		if (id != other.id)
			return false;
		return true;
	}

}